<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>激活帐户</title>
<style type="text/css">
<!--
td {  font-size: 9pt}
-->
</style>
</head>
<body bgcolor="#ffffff" topmargin=0>

<?php

include 'config.php';

	$username = $_GET['userid'];
	$code = $_GET['code'];
	$userinfo = $configValues['CONFIG_DB_TBL_DALOUSERINFO'];

	if($username == "" || $code == ""){
		echo "<br/><br/>
		Invalid request.<br/>";
		exit;
	}

	include 'opendb.php';

	/* check if user already exists */
	$sql = "SELECT username, creationdate, activationcode, activated FROM $userinfo WHERE username = '$username'";
	$res = $dbSocket->query($sql);
	if ($res->numRows() == 0){
		echo "<br/><br/>
		The user account doesn't exists.<br/>";
		$dbSocket->disconnect();
		echo '<p><a href="index.php">返回主页</a>';
		exit;
	}
	$row = $res->fetchRow();


	$creationdate = $row[1];
	$timestamp1 = strtotime($creationdate);
	$timestamp2 = time();

	/* 259200 = 3 days */
	if($timestamp2 > $timestamp1 + 259200){
		echo "<br/><br/>
		链接已失效。<br/>";
		$dbSocket->disconnect();
		echo '<p><a href="index.php">返回主页</a>';
		exit;
	}

	$activated = $row[3];
	if($activated){
		echo "<br/><br/>
		此用户已经激活，请不要重复激活。<br/>";
		$dbSocket->disconnect();
		echo '<p><a href="index.php">返回主页</a>';
		exit;
	}
	$activationcode = $row[2];
	if($activationcode != $code){
		echo "<br/><br/>
		验证码错误。请登录帐户选择重新发送激活Email。<br/>";
		$dbSocket->disconnect();
		echo '<p><a href="index.php">返回主页</a>';
		exit;
	}

	/* accoutn activated, update userinfo */
	$sql = "UPDATE userinfo SET activationcode='', activated='1' WHERE username = '$username'";
	$res = $dbSocket->query($sql);

	/* move user to freeuser group */
	$sql = "UPDATE radusergroup SET groupname='freeuser' WHERE username = '$username'";
	$res = $dbSocket->query($sql);

	$dbSocket->disconnect();

	echo "<br/><br/>帐户激活成功！<br/>";
	echo '<p><a href="index.php">返回主页</a>';
	echo "</body></html>";
?>
</body>

